package client;

import dao.EmployeeDAO;
import dao.ProductDAO;
import model.Employee;
import model.Product;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.List;

/**
 * 员工客户端
 */
public class EmployeeClient {
    private static final String SERVER_ADDRESS = "localhost";
    private static final int SERVER_PORT = 8888;
    private static String employeeId;
    
    public static void main(String[] args) {
        try (Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {
            
            System.out.println("欢迎使用超市管理系统-员工端");
            
            // 登录流程
            if (!login(stdIn)) {
                System.out.println("登录失败，程序退出");
                return;
            }
            
            // 发送角色信息给服务器
            out.println("employee:" + employeeId);
            
            // 启动一个线程接收服务器消息
            Thread receiverThread = new Thread(() -> {
                try {
                    String response;
                    while ((response = in.readLine()) != null) {
                        System.out.println("\n服务器消息: " + response);
                        System.out.print("请输入命令: ");
                    }
                } catch (IOException e) {
                    System.err.println("接收消息错误: " + e.getMessage());
                }
            });
            receiverThread.start();
            
            // 处理用户输入
            System.out.println("登录成功！可用命令：");
            System.out.println("1. 查询库存: queryStock:商品ID");
            System.out.println("2. 查看低库存商品: lowStock:阈值");
            System.out.println("3. 发送补货请求: restock:商品ID:数量:供应商ID");
            System.out.println("4. 退出: exit");
            
            String userInput;
            while ((userInput = stdIn.readLine()) != null) {
                if ("exit".equalsIgnoreCase(userInput)) {
                    out.println(userInput);
                    break;
                } else if (userInput.startsWith("queryStock:")) {
                    // 查询库存
                    String[] parts = userInput.split(":");
                    if (parts.length == 2) {
                        ProductDAO productDAO = new ProductDAO();
                        Product product = productDAO.getProductById(parts[1]);
                        if (product != null) {
                            System.out.println("商品ID: " + product.getProductId());
                            System.out.println("商品名称: " + product.getName());
                            System.out.println("售价: " + product.getPrice());
                            System.out.println("当前库存: " + product.getStock());
                        } else {
                            System.out.println("未找到该商品");
                        }
                    } else {
                        System.out.println("命令格式错误，正确格式: queryStock:商品ID");
                    }
                } else if (userInput.startsWith("lowStock:")) {
                    // 查看低库存商品
                    String[] parts = userInput.split(":");
                    if (parts.length == 2) {
                        try {
                            int threshold = Integer.parseInt(parts[1]);
                            ProductDAO productDAO = new ProductDAO();
                            List<Product> products = productDAO.getLowStockProducts(threshold);
                            System.out.println("库存低于" + threshold + "的商品:");
                            for (Product p : products) {
                                System.out.println(p.getProductId() + " - " + p.getName() + " : " + p.getStock());
                            }
                        } catch (NumberFormatException e) {
                            System.out.println("阈值必须是数字");
                        }
                    } else {
                        System.out.println("命令格式错误，正确格式: lowStock:阈值");
                    }
                } else if (userInput.startsWith("restock:")) {
                    // 发送补货请求给服务器
                    String[] parts = userInput.split(":");
                    if (parts.length == 4) {
                        out.println("restockRequest:" + parts[1] + ":" + parts[2] + ":" + parts[3]);
                    } else {
                        System.out.println("命令格式错误，正确格式: restock:商品ID:数量:供应商ID");
                    }
                } else {
                    System.out.println("未知命令，请重新输入");
                }
                System.out.print("请输入命令: ");
            }
            
            receiverThread.interrupt();
            
        } catch (IOException e) {
            System.err.println("无法连接到服务器: " + e.getMessage());
        }
    }
    
    // 登录方法
    private static boolean login(BufferedReader stdIn) throws IOException {
        EmployeeDAO employeeDAO = new EmployeeDAO();
        
        System.out.print("请输入您的员工ID: ");
        employeeId = stdIn.readLine();
        
        System.out.print("请输入密码: ");
        String password = stdIn.readLine();
        
        Employee employee = employeeDAO.login(employeeId, password);
        return employee != null;
    }
}
    